package com.ynunicom.midplatform.sys.user;

import com.ynunicom.midplatform.common.result.ResponseEntity;
import com.ynunicom.midplatform.common.util.Md5Util;
import com.ynunicom.midplatform.sys.role.SysRole;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户 Controller
 *
 * @author duwei
 */
@Controller
public class SysUserController {

    private final SysUserService userInfoService;

    @Autowired
    public SysUserController(SysUserService userInfoService) {
        this.userInfoService = userInfoService;
    }


    @RequiresPermissions("user:list")
    @PostMapping("/user/list")
    @ResponseBody
    public List<SysUser> list() {
        return userInfoService.findAll();
    }

    @PostMapping("/user/permissions")
    @ResponseBody
    public Object permissions() {
        try {
            Subject currentUser = SecurityUtils.getSubject();
            SysUser sysUser = (SysUser) currentUser.getPrincipal();
            return ResponseEntity.success("获取用户权限成功！", sysUser);
        } catch (Exception e) {
            return ResponseEntity.failed("获取用户权限异常！", e.getMessage());
        }
    }


    @RequiresPermissions("user:del")
    @PostMapping("/user/delUserInfo")
    @ResponseBody
    public Object delUserInfo(SysUser sysUser) {
        try {
            userInfoService.delete(sysUser);
            return ResponseEntity.success("删除用户成功！", sysUser);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.failed("删除用户异常！", e.getMessage());
        }
    }
}
